layui.use(['upload', 'element', 'layer'], function () {
    var $ = layui.jquery
        , upload = layui.upload
        , element = layui.element
        , layer = layui.layer;

    //获取本地的
    var user = localStorage.getItem("user");
    user = JSON.parse(user);
    var id = user.data.userId;
    //点击修改
    $("#btn").click(function () {
        var pwd = $("#pwd").val();
        var surepwd = $("#surepwd").val();
        if (pwd!=null && surepwd!= null){
            $.ajax({
                url:"/login/updateUser",
                type: 'post',
                dataType:"json",
                data:{"userId":id,"password":pwd},
                success: function () {
                    layer.msg("修改成功");
                }
            })
        }else{
            alert("密码不能为空");
        }
    });

    //img 获取数据库字段路径 查询字段放进去
    upload.render({
        elem: "#demo1",
        url: "/Upload/upload",
        bindAction: '#singleUpload',//指定一个按钮触发上传
        method: "post",
        field:'file',//设定文件域的字段名
        accept: 'file',//上传文件类型
        choose: function(obj){//选择文件后的回调函数
            //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
            obj.preview(function(index, file, result){
                //var newName = "/upload/"+file.name;
                //实现图片预览
                $("#demo1").attr("src",result);
                //导航栏同步
                parent.$("#imgSrc").attr('src',result);

            });
        },
        done : function(res,index,upload){
            console.log(res);
            console.log(res.data);
            //获取到上传文件的名字
            $.ajax({
                url: '/login/uploadUserHead',
                type: 'post',
                dataType: 'json',
                data: {"userId": id, "acatarUrl": res.data},
                success:function(data){
                    console.log("修改成功");
                }
            });
        },
    });


    //查询所有数据
    chaxun();
    function chaxun(){
        $.ajax({
            url: "/login/selectUserById",
            type: 'post',
            dataType: "json",
            data: {"id": id},
            success: function (data) {
                $("#tel").val(user.data.account);
                $("#eamil").val(user.data.email);
                $("#demo1").attr('src',user.data.acatarUrl);
            }
        })
    }

    //点击返回
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });

    refresh = function refresh(){
        location.reload();
    }

});


